/*
 * @lc app=leetcode.cn id=332 lang=cpp
 *
 * [332] 重新安排行程
 *
 * https://leetcode-cn.com/problems/reconstruct-itinerary/description/
 *
 * algorithms
 * Medium (37.69%)
 * Likes:    128
 * Dislikes: 0
 * Total Accepted:    8.2K
 * Total Submissions: 21.7K
 * Testcase Example:  '[["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]'
 *
 * 给定一个机票的字符串二维数组 [from,
 * to]，子数组中的两个成员分别表示飞机出发和降落的机场地点，对该行程进行重新规划排序。所有这些机票都属于一个从JFK（肯尼迪国际机场）出发的先生，所以该行程必须从
 * JFK 出发。
 * 
 * 说明:
 * 
 * 
 * 如果存在多种有效的行程，你可以按字符自然排序返回最小的行程组合。例如，行程 ["JFK", "LGA"] 与 ["JFK", "LGB"]
 * 相比就更小，排序更靠前
 * 所有的机场都用三个大写字母表示（机场代码）。
 * 假定所有机票至少存在一种合理的行程。
 * 
 * 
 * 示例 1:
 * 
 * 输入: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
 * 输出: ["JFK", "MUC", "LHR", "SFO", "SJC"]
 * 
 * 
 * 示例 2:
 * 
 * 输入: [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
 * 输出: ["JFK","ATL","JFK","SFO","ATL","SFO"]
 * 解释: 另一种有效的行程是 ["JFK","SFO","ATL","JFK","ATL","SFO"]。但是它自然排序更大更靠后。
 * 
 */
#include <vector>
#include <string>
#include <unordered_map>
#include <map>
#include <stack>

using namespace std;

// @lc code=start
class Solution {
public:
    vector<string> findItinerary(vector<vector<string>>& tickets) {
        unordered_map<string,map<string,int>> adjacent;
        vector<string> path;
        stack<string> s;
        s.push("JFK");
        for(auto& p:tickets)
            adjacent[p[0]][p[1]]++;
        while (s.size()) {
            bool bottom = false;
            while (!bottom) {
                bottom = true;
                for(auto& [next, count]:adjacent[s.top()]){
                    if(count>0){
                        s.push(next);
                        count--;
                        bottom=false;
                        break;
                    }
                }
            }
            path.insert(path.begin(), s.top());
            s.pop();
        }
        return path;
    }
};
// @lc code=end

